package com.example.bookkeeping1220.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bookkeeping1220.R;
import com.example.bookkeeping1220.db.CharactersBean;

import java.util.List;

public class CharactersAdapter extends BaseAdapter {
    Context context;
    List<CharactersBean> mDatas;
    LayoutInflater layoutInflater;

    public CharactersAdapter(Context context){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public CharactersAdapter(Context context, List<CharactersBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ViewHolder{
        public ImageView headIv;
        public TextView nameTv;
        ViewHolder(View view){
            headIv = view.findViewById(R.id.item_character_head);
            nameTv = view.findViewById(R.id.item_character_name);
        }
    }
    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        CharactersAdapter.ViewHolder holder = null;
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.item_characters_one_character_lv,parent,false);
            holder = new CharactersAdapter.ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (CharactersAdapter.ViewHolder) convertView.getTag();
        }
        CharactersBean bean = mDatas.get(i);
        holder.nameTv.setText(bean.getName());
        holder.headIv.setImageResource(bean.getsImageId());
        return convertView;
    }
}
